<?php


namespace app\api\controller\v1;


use app\api\model\BikeMgr;
use app\api\model\BikeModel;
use app\api\model\BikeOperRecordModel;
use app\api\model\CityModel;
use app\api\model\ParkModel;
use app\api\model\PayModel;
use app\common\controller\UserBaseController;
use app\common\library\dingding\AliDingDing;
use app\common\library\StatModel;
use app\common\library\XiaoAnEcuModel;
use think\Db;

class BackendController extends UserBaseController
{
    function check_wxpay_billAction()
    {
        //hilog::set_pay_log_path();
        $this->paramlist = array(
            'date' => 'optdate',
            'token' => ['optstring', 'SUOZHANGBIEKAIQIANG'],
        );

        $this->check_params();
        $this->check_token();

        $date = date('Ymd', strtotime("-2 day"));
        if ($this->argv['date']) {
            $date = str_replace('-', '', $this->argv['date']);
        }
        (new PayModel())->check_wxpay_order($date);
    }

    function check_alipay_billAction()
    {
        //hilog::set_pay_log_path();
        $this->paramlist = array(
            'date' => 'optdate',
            'token' => ['optstring', 'SUOZHANGBIEKAIQIANG'],
        );

        $this->check_params();
        $this->check_token();

        $date = date('Y-m-d', strtotime("-1 day"));
        if ($this->argv['date']) {
            $date = $this->argv['date'];
        }
        (new PayModel())->check_alipay_order($date);
    }

    function set_city_bike_countAction()
    {
        $this->paramlist = array(
            'token' => ['optstring', 'SUOZHANGBIEKAIQIANG'],
        );

        $this->check_params();
        $this->check_token();


        $cities = (new CityModel())->query_all(0, 100);

        foreach ($cities as $city) {
            $city_id = $city['id'];
            $count = (new BikeModel())->query_bike_count($city_id);
            (new StatModel())->bike_count_set($city_id, $count);
        }

    }

    function check_alertAction()
    {
        $this->paramlist = [
            'token' => ['optstring', 'SUOZHANGBIEKAIQIANG'],
        ];

        $this->check_params();
        $this->check_token();

        $l = BikeModel::instance()->query_list_to_check();
        foreach ($l as $e) {
            (new BikeMgr())->on_bike_info_update($e['imei'], []);
        }
    }

    function update_bike_bluetoothAction()
    {
        $this->paramlist = array(
            'bike_sn' => 'string',
            'token' => ['optstring', 'SUOZHANGBIEKAIQIANG'],
        );

        $this->check_params();
        $this->check_token();

        $bike_info = BikeModel::instance()->get_info_by_sn($this->argv['bike_sn']);
        if (empty($bike_info)) {
            $this->throw_myexception(X_ERROR, "shit!");
        }

        if ($bike_info['ecu_brand'] != 'TBIT')
        {
            $new_name = null;
            if ($bike_info['sn'] != $bike_info['bluetooth_name']) {
                $new_name = $bike_info['sn'];
            }
            $new_token = rand(0, 0xffffff);
            // $new_token = 168;

            $ret = (new XiaoAnEcuModel())->set_bluetooth($bike_info['imei'], $new_token, $new_name);
            if ($ret[0] === 0) {
                (new BikeModel())->update_bluetooth_name_token($bike_info['id'], $new_name, strval($new_token));
            } else {
                $this->throw_myexception(X_ERROR, $ret[1]);
            }
        }
        else
        {
            $ecu_info = (new BikeModel())->get_ecu_info_by_sn($bike_info['ecu_sn']);
            if($ecu_info){
                (new BikeModel())->update_bluetooth_name_token($bike_info['id'], null,$ecu_info['bluetooth_token']);
            }

        }

    }

    function bat_update_bike_bluetoothAction()
    {
        $this->paramlist = array(
            'city_id' => ['optstring', '350001'],
            'token' => ['optstring', 'SUOZHANGBIEKAIQIANG'],
        );
        $cd = [
            'is_bluetooth_set' => 0,
            'city_id' => $this->argv['city_id'],
        ];

        $city_id = null;
        $city_id = $this->argv['city_id'];
        $l = (new BikeModel())->get_list($city_id, $cd, 0, 10);

        $set = [];
        foreach ($l as $bike_info) {
            $new_name = null;
            if ($bike_info['sn'] != $bike_info['bluetooth_name']) {
                $new_name = $bike_info['sn'];
            }
            $new_token = rand(0, 0xffffff);
            // $new_token = 168;

            $ret = (new XiaoAnEcuModel())->set_bluetooth($bike_info['imei'], $new_token, $new_name);
            if ($ret[0] === 0) {
                (new BikeModel())->update_bluetooth_name_token($bike_info['id'], $new_name, $new_token);
                $set[] = $bike_info['sn'];
            }
        }
        $this->resp['sns'] = $set;
    }

    function revise_park_bike_countAction()
    {
        $this->paramlist = [
            'city_id' => ['optstring', '362501'],
        ];
        $this->check_params();
        $park_id_bike_count_map = [];
        $bike_list = (new BikeModel())->get_list($this->argv['city_id'], [], 0, 1000);
        foreach ($bike_list as $bike_info) {
            if ($bike_info['park_state'] == BIKE_PARK_STAT_PARK && !empty($bike_info['park_id'])) {
                if (isset($park_id_bike_count_map[$bike_info['park_id']])) {
                    $park_id_bike_count_map[$bike_info['park_id']]++;
                } else {
                    $park_id_bike_count_map[$bike_info['park_id']] = 1;
                }
            }
        }

//        $st = "update tb_park set bike_count = 0 where city_id = ?";
//        $beans = [$this->argv['city_id']];
//
//        hisql::db()->execute($st, $beans);

        Db::table('tb_park')->where('city_id',$this->argv['city_id'])->update(['bike_count'=>0]);

        foreach ($park_id_bike_count_map as $park_id => $bike_count) {
            (new ParkModel())->incr_bike_count($this->argv['city_id'], $park_id, 0, $bike_count);
        }
        $this->resp['map'] = $park_id_bike_count_map;
    }

    function archive_bike_oper_recordAction()
    {
        (new BikeOperRecordModel())->archive(30, 1000);
    }

    function start_ecu_event_consumerAction()
    {
        ProcessMgr::start_xiaoan_ecu_event_consumer(20);
        ProcessMgr::start_tbit_ecu_event_consumer(5);
    }

    function export_fin_csvAction()
    {
        $this->paramlist = [
            'start_date' => 'optdate',
            'city_id' => 'optstring',
            'end_date' => 'optdate',
        ];
        $this->check_params();

        $city_id = null;
        $cities = [];
        if(empty($this->argv['city_id'])){
            $cities = (new CityModel())->query_all(0, 100);
        }else{
            $city = (new  CityModel())->query_info($this->argv['city_id']);
            if(empty($city)){
                $this->throw_myexception(X_ERROR, "city?");
            }
            $cities[] = $city;
        }

        $last_month_start = date('Y-m-01', strtotime('-1 month'));
        $last_month_end = date('Y-m-t', strtotime('-1 month')) . " 23:59:59";

        if(!empty($this->argv['start_date'])){
            $last_month_start = $this->argv['start_date'] . " 00:00:00";
        }
        if (!empty($this->argv['end_date'])) {
            $last_month_end = $this->argv['end_date'] . " 23:59:59";
        }

        foreach ($cities as $city) {
            (new FinReportModel())->user_order_export($city['name'], $city['id'], $last_month_start, $last_month_end);
            (new FinReportModel())->user_charge_order_list_export($city['name'], $city['id'], $last_month_start, $last_month_end);
            (new FinReportModel())->user_membership_order_list_export($city['name'], $city['id'], $last_month_start, $last_month_end);
            (new FinReportModel())->underbalance_user_export($city['name'], $city['id']);
        }
    }

    function morning_callAction()
    {
        $cities = (new CityModel())->query_all(0, 100);
        $yesterday = date('Y-m-d', strtotime("-1 day"));
        $news = $yesterday . "\n";
        $count_total = 0;
        $amount_total = 0;
        foreach ($cities as $city) {
            $order_count = (new StatModel())->user_ride_count($city['id'], $yesterday);
            $order_amount = (new StatModel())->user_ride_amount($city['id'], $yesterday);
            if ($order_amount == 0 || $order_count == 0)
                continue;

            $order_amount = $order_amount / 100;
            $count_total += $order_count;
            $amount_total += $order_amount;
            $news = $news . "{$city['name']}\t订单：{$order_count}  \t金额：{$order_amount}元 \n";
        }

        $news = $news . "总计\t订单: {$count_total}  \t金额：{$amount_total}元 \n";

        $ding = new AliDingDing(config('app.morning_call_ding_ding_token'));
        $ding->assign('text', $news)
            ->at(false)
            ->send();
    }
}
